//package appapi.controller.mqtt;
//
//import appapi.Global.BusinessException;
//import appapi.Request.Basic.StaffReq;
//import appapi.common.ResponseBean;
//import appapi.config.MqttFactory;
//import appapi.entity.Zhongjiao.StaffDto;
//import appapi.entity.auth.StaffEntity;
//import appapi.entity.mqtt.*;
//import appapi.service.TerminalService;
//import appapi.utils.RedisUtils;
//import com.github.pagehelper.PageInfo;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
//import org.eclipse.paho.client.mqttv3.MqttException;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.*;
//
//import javax.annotation.Resource;
//import java.util.Date;
//import java.util.List;
//import java.util.UUID;
//
///**
// * @version: java version 1.8
// * @Author: Ygp Work
// * @description:
// * @date: 2023-07-04 10:42
// */
//@RestController
//@RequestMapping("/api/mqtt")
//@Api(tags = "硬件传输接口")
//public class MqttController {
//    @Autowired
//    private TerminalService terminalService;
//    @Autowired
//    private RedisUtils redisUtils;
//
//    @Resource
//    MqttFactory mqttFactory;
//
//    @CrossOrigin
//    @ApiOperation(value = "实时数据获取")
//    @GetMapping("/realDataList")
//    public ResponseBean getValueFromRedis(TopicReq req) {
//        String value = (String) redisUtils.get(req.getEquiNo());
//        if (value != null) {
//            return ResponseBean.success(value);
//        } else {
//            return ResponseBean.fail("没有查到这台设备的数据");
//        }
//    }
//
//
//    @ApiOperation(value = "根据终端编号与终端标头查询数据")
//    @GetMapping("/getTerm")
//    @CrossOrigin
//    public ResponseBean getTerminalById(TerminalReq req) {
//        List<Terminal> terminalById = terminalService.getTerminalById(req);
//        if (terminalById != null) {
//            return ResponseBean.success(terminalById);
//        }
//        return ResponseBean.fail("查询失败");
//    }
//
//
//    @ApiOperation(value = "修改配置")
//    @PostMapping("/updateTerminal")
//    @CrossOrigin
//    public ResponseBean updateTerminal(@RequestBody TermialReq req) throws Exception {
//        try {
//            mqttFactory.setTopic(req.getTopic());
//            String s = UUID.randomUUID().toString();
//            mqttFactory.sendMessage(s, req.getTopic(), req.getData());
//            return ResponseBean.success("发送成功,如果页面上没有更新，请重新发送，可能是网络延迟");
//        } catch (Exception e) {
//            System.out.println(e);
//            return ResponseBean.fail("请检查输入的值或者网络延迟请再次发送");
//        }
//    }
//
//    @ApiOperation(value = "传输开始")
//    @PostMapping("/transmissionStart")
//    @CrossOrigin
//    public ResponseBean transmissionStart(@RequestBody TermialReq req) {
//        String s = UUID.randomUUID().toString();
//        //存入设备号与当前时间
////        redisUtils.set("Online:".concat(req.getTermId()), new Date());
//        try {
//            mqttFactory.setTopic(req.getTopic());
//            mqttFactory.sendMessage(s, req.getTopic(), req.getData());
//            return ResponseBean.success("成功");
//        } catch (MqttException e) {
//
//            return ResponseBean.fail("失败");
//        }
//    }
//
//    @GetMapping("devicesPageList")
//    @ApiOperation(value = "分页查询所有的设备列表")
//    @CrossOrigin
//    public ResponseBean<List<Terminals>> getPageDevices(Terminals terminal) {
//        try {
//            if (terminal.getCurrPage() == null || terminal.getPageSize() == null) {
//                terminal.setCurrPage(1);
//                terminal.setPageSize(10);
//            }
//            PageInfo pageInfo = terminalService.getTerminalAll(terminal);
//            return ResponseBean.success(pageInfo);
//        } catch (BusinessException exception) {
//            return ResponseBean.fail("设备信息获取异常");
//        }
//    }
//
//
//    @GetMapping
//    @ApiOperation(value = "查询所有的设备列表")
//    @CrossOrigin
//    public ResponseBean<List<Terminal>> getDevices(Terminal terminal) {
//        List<Terminal> terminalList = terminalService.getTerminalAlls(terminal);
//        int size = terminalList.size();
//        if (!terminalList.isEmpty()) {
//            return ResponseBean.success(size, terminalList);
//        } else {
//            return ResponseBean.fail("查询失败，设备列表为空");
//        }
//    }
//
//
//    @GetMapping("getUserAll")
//    @ApiOperation(value = "查询所有的用户")
//    @CrossOrigin
//    public ResponseBean<List<StaffDto>> getUsers() {
//        List<StaffDto> userList = terminalService.getUserall();
//        int size = userList.size();
//        if (!userList.isEmpty()) {
//            return ResponseBean.success(size, userList);
//        } else {
//            return ResponseBean.fail("查询失败，用户列表为空");
//        }
//    }
//
//    @ApiOperation(value = "心跳")
//    @PostMapping("/heartbeat")
//    @CrossOrigin
//    public ResponseBean heartbeat(@RequestBody TermialReq req) {
//        String s = UUID.randomUUID().toString();
//        //存入设备号与当前时间
//        Object online = redisUtils.get("Online:".concat(req.getTermId()));
//
//        try {
//            mqttFactory.setTopic(req.getTopic());
//            mqttFactory.sendMessage(s, req.getTopic(), req.getData());
//            if (online == null) {
//                return ResponseBean.success("设备不在线");
//            }
//            return ResponseBean.success("成功");
//        } catch (MqttException e) {
//            return ResponseBean.fail("失败");
//        }
//    }
//
//    @ApiOperation(value = "传输结束")
//    @PostMapping("/transmissionEnd")
//    @CrossOrigin
//    public ResponseBean transmissionEnd(@RequestBody TermialReq req) {
//        String s = UUID.randomUUID().toString();
//        //存入设备号与当前时间
//        try {
//            mqttFactory.setTopic(req.getTopic());
//            mqttFactory.sendMessage(s, req.getTopic(), req.getData());
//            return ResponseBean.success("成功");
//        } catch (MqttException e) {
//            return ResponseBean.fail("失败");
//        }
//    }
//}